/**
 * Created by sxx on 2017/5/1.
 */
import "./index.less";
// import "../../../unit/common/js/common";

// $(document).click('.J_submit', function(){
//     console.log('success');
// });

$('.J_add-user-form').validate({
    errorElement: 'label', //default input error message container
    errorClass: 'help-inline', // default input error message class
    focusInvalid: false, // do not focus the last invalid input
    rules: {
        username: {
            required: true
        },
        password: {
            required: true
        }
    },

    messages: {
        username: {
            required: "请填写用户名"
        },
        password: {
            required: "请填写密码"
        }
    },

    invalidHandler: function (event, validator) { //display error alert on form submit
        $('.alert-error', $('.J_add-user-form')).show();
    },

    highlight: function (element) { // hightlight error inputs
        $(element)
            .closest('.control-group').addClass('error'); // set error class to the control group
    },

    success: function (label) {
        label.closest('.control-group').removeClass('error');
        label.remove();
    },

    errorPlacement: function (error, element) {
        error.addClass('help-small no-left-padding').insertAfter(element.closest('.input-icon'));
    },

    submitHandler: function (form) {
        let $form = $(form);
        let username = $form.find('.J_username')[0].value;
        let password = $form.find('.J_password')[0].value;
        let nick = $form.find('.J_nick')[0].value;
        let token = sessionStorage.getItem('supplier_token');

        let data = {
            "acntTyp": "普通用户",
            "nam": nick,
            "mim": password,
            "usrNam": username
        };

        $.ajax({
            url: HOST_ADDR + '/cpmp/splrSlfMng/crtAcnt',
            type: 'post',
            data: JSON.stringify(data),
            dataType: 'json',
            contentType:"application/json",
            access_token: token,
            headers: {
                'access_token': token,
                'login_type': 'splr'
            },
            success: function(resp){
                if(resp.success){
                    console.log(resp);
                    // //页面跳转到index
                    alert('新增用户成功');
                    location.href = '/supplier_self/user_manage.html';
                }else{
                    alert(resp.msgInf);
                    location.href = '/supplier_self/user_manage.html';
                }

            },
            error:function(err){
                console.log(err);
            }
        });
    }
});

$('.J_add-user-form .J_cancel').click(function(){
    location.href = '/supplier_self/user_manage.html';
});

